<?php
namespace Modules\Activity\Entities\Seckill;

use Modules\Activity\Entities\BaseActivityModel;
use Illuminate\Database\Eloquent\Builder;
use ThrowException;

// 一翻赏
class Activity extends BaseActivityModel
{
    protected $table = 'seckill_activity';

    public $activityType = 'seckill';
    public $activityTypeText = '秒杀';

    // protected $guarded = ['union_status', 'union_status_text', 'type_text', 'sales'];

    // protected $appends = ['union_status', 'union_status_text'];

    protected $casts = [
    ];

    public function user_group()
    {
        return $this->belongsTo('\Modules\User\Entities\UserGroup', 'user_group_id');
    }

    public function getType($type) 
    {
        $map = [
            // 'base_sku_type' => 'yifanshang_sku',
            'node_type' => 'seckill'
        ];

        return @$map[$type];
    }

    // 秒杀活动是否包含某个node
    public function isIncludeNode($node) 
    {
        return $node->getNodeEntity()->uuid === $this->node_uuid;
    }

    public function checkCanBuy($node, $total)
    {
        return $this->isIncludeNode($node) && $this->stock >= $total;
    }

    public function getSeckillInfo()
    {
       $info = [
            'uuid' => $this->uuid,
            'title' => '秒杀活动',
            'status' => $this->union_status,
            'score_price' => $this->discount_score_price,
            'money_price' =>  $this->discount_money_price,
            'user_group_id' => $this->user_group_id,
            // 'discount_money_price' => $this->discount_money_price,
            // 'discount_score_price' => $this->discount_score_price,
            'start_at' => $this->start_at,
            'end_at' => $this->end_at,
            'stock' => $this->stock,
        ];

        return $info; 
    }

    // 对某个sku是否有效
    // public function isUsableForSku($sku)
    // {
    //     return @$this->activity_info->product_uuid === $sku->parent_product->uuid;
    // }
}
